package helpers;

import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class UrlHelper{
	public static String getServerUrl(HttpServletRequest request) {
		String url = "";
		String serverName = request.getServerName();
		String port = Integer.toString(request.getServerPort());
		if (serverName.contains("localhost")) {
			url += "http://" + serverName + ":" + port;
		} else {
			url += "http://" + serverName;
		}
		return url;
	}
	
	public static String getDomainServer(HttpServletRequest request) {
		String name = "";
		
		String serverName = request.getServerName();
		String port = Integer.toString(request.getServerPort());
		
		serverName = fomatDomains(serverName);
		if (serverName.equals("localhost")) {
			name += serverName + ":" + port;
		} else {
			name += serverName;
		}
		
		return name;
	}
	
	public static String getDomainLanguage(String url) {
		String name = "";
		
		url = fomatDomains(url);
		
		if (url.contains("appspot.com")) {
			Pattern p = Pattern.compile("([a-zA-Z0-9-]+)([.][a-zA-Z_0-9-]+){1}([.][a-zA-Z]+){1}$");
			Matcher m = p.matcher(url);

			if (m.find()) {
			    name = m.group(1) + ".appspot.com";
			}
		} else {
			name = getMainDomain(url);
		}
		
		return name;
	}
	
	public static String getDomainLanguage(HttpServletRequest request) {
		String name = "";
		
		String url = request.getServerName();
		
		if (url.contains("appspot.com")) {
			Pattern p = Pattern.compile("([a-zA-Z0-9-]+)([.][a-zA-Z_0-9-]+){1}([.][a-zA-Z]+){1}$");
			Matcher m = p.matcher(url);

			if (m.find()) {
			    name = m.group(1) + ".appspot.com";
			}
		} else {
			name = getMainDomain(url);
		}
		
		return name;
	}
	
	
	public static boolean checkDomain(String domain) {
        ResourceBundle resource = ResourceBundle.getBundle("extension_domain",
                        new Locale("en"));
        String re_domain = domain;
        
        if(!Pattern.matches("^[a-zA-Z0-9-]+([.][a-zA-Z_0-9-]+)*([.][a-zA-Z]+)+", re_domain))
        {
        	return false;
        }
        
        String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
                re_domain = re_domain
                                .replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }

        if (re_domain.length() != domain.length()) {
                return true;
        }

        return false;
	}
	
	public static Boolean checkIsSubDomain(String domain) {
		ResourceBundle resource = ResourceBundle.getBundle("extension_domain",
                new Locale("en"));
		String re_domain = domain;
		
		String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
                re_domain = re_domain
                                .replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }
        
        if (re_domain.length() != domain.length()) {
        	if (Pattern.matches("^[a-zA-Z0-9-]+$", re_domain.replace("www.", ""))) {
            	return false;
            }
        }
		
		return true;
	}
	
	public static String getMainDomain(String domain) {
		String domainName = "";
		ResourceBundle resource = ResourceBundle.getBundle("extension_domain",
                new Locale("en"));
		String re_domain = domain;
		String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
                re_domain = re_domain
                                .replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }
        
        if (re_domain.length() != domain.length()) {
        	
        	String[] d1 = domain.split("[.]");
        	String[] d2 = re_domain.split("[.]");
        	
        	for (int i = d2.length -1; i < d1.length; i++) {
        		if (!domainName.isEmpty())
        			domainName +=".";
        		domainName += d1[i];
        		
        	}
        	
        } else {
        	domainName = domain;
        }
		
		return domainName;
	}
	
	public static String fomatSitemap(String url)
	{
		if (url != null)
			url = "http://"+url.toLowerCase().replaceAll("(http|https)+://", "").trim().replaceAll("[/]+$", "");
		return url;
	}
	
	public static String fomatDomains(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
	
	public static String getSubDomain(String domain) {
		String subDomain = "";
		domain = fomatDomains(domain);
		String mainDomain = getMainDomain(domain);
		
		subDomain = domain.replaceAll("[.]?("+mainDomain+")+", "");
		
		return subDomain;
	}
	
	public static String getFirstSubDomain(String domain) {
		String firstSubDomain = "";
		
		String subDomain = getSubDomain(domain);
		
		if (!subDomain.isEmpty()) {
			String[] d1 = subDomain.split("[.]");
			firstSubDomain = d1[0];
		}
		
		return firstSubDomain;
	}
	
	public static String getIdFromInfoUrl(String str) {
		String ip = "";
		String regex = "[0-9A-Za-z]+$";
		ArrayList<String> ips = StringHelper.findString(regex, str);
		if (ips.size() > 0) {
			ip = ips.get(0);
		}
		return ip;
	}
	
}